home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / show movie / show movie.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.0 KB  |  314 lines

  1. /*
  2.     File:        Show Movie.c
  3.  
  4.     Contains:        main and event loop
  5.                             
  6.                             Allows you to open and display movies
  7.                             in a window.
  8.  
  9.     Written by: Jason Hodges-Harris & Don Swatman    
  10.  
  11.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 8/17/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 
  24.  
  25. */
  26. #include <Types.h>
  27. #include <Memory.h>
  28. #include <Quickdraw.h>
  29. #include <Fonts.h>
  30. #include <Events.h>
  31. #include <Menus.h>
  32. #include <Windows.h>
  33. #include <TextEdit.h>
  34. #include <Dialogs.h>
  35. #include <OSUtils.h>
  36. #include <ToolUtils.h>
  37. #include <SegLoad.h>
  38. #include <Movies.h>
  39. #include <DiskInit.h>
  40.  
  41. // Show Movie Headers
  42. #include "MenuStuff.h"
  43. #include "WindStuff.h"
  44. #include "MovieStuff.h"
  45. #include "MoviePrefs.h"
  46.  
  47. //----------------------------------------------
  48. // Prototypes
  49. //----------------------------------------------
  50.  
  51.     // Inititalization
  52. OSErr InitMacStuff(void);
  53. void InitGlobals(void);
  54. void KillGlobals(void);
  55.     
  56.     // Events
  57. void EventLoop (void);
  58.  
  59.  
  60. //==============================================
  61. //  main
  62. //==============================================
  63.  
  64. void main(void)
  65. {
  66.     OSErr theErr;
  67.     
  68.     theErr = InitMacStuff();
  69.     if (!theErr)
  70.     {
  71.         InitGlobals();    // initialise globals
  72.         
  73.         EventLoop();      // Call the main event loop.
  74.  
  75.         KillGlobals();    // Tear down any globals
  76.  
  77.         ExitToShell();    // Quit the application.
  78.  
  79.     }
  80.     
  81. }
  82.  
  83.  
  84. //==============================================
  85. // Initialisation Stuff
  86. //==============================================
  87.  
  88. //----------------------------------------------
  89. // InitMacStuff
  90. //
  91. // Initialise the Mac Tool Box and set up the memory
  92. //----------------------------------------------
  93.  
  94. OSErr InitMacStuff(void)
  95. {
  96.     long     *appSize;
  97.     OSErr          theErr = noErr;
  98.         
  99. // This decreases the application heap by 16k
  100. //       (which increases the stack by 16k.)
  101.     appSize = (long*)(GetApplLimit());
  102.     SetApplLimit (appSize -16384);
  103.  
  104. // Expand the heap so code segments load at the top.
  105.     MaxApplZone();
  106.  
  107. // allocate more master pointers
  108.     MoreMasters();
  109.     MoreMasters();
  110.  
  111. // ****** Init Mac toolbox ******
  112.     InitGraf (&qd.thePort);
  113.     InitFonts();
  114.     InitWindows();
  115.     InitMenus();
  116.     TEInit();
  117.     InitDialogs(0L);
  118.     InitCursor();
  119.  
  120. // initialise movie toolbox
  121.     theErr = EnterMovies();
  122.     if (theErr)
  123.     {
  124.         DebugStr("\pError in initialising movie toolbox.");
  125.     }
  126.     return(theErr);
  127. }
  128.  
  129. //----------------------------------------------
  130. // InitGlobals
  131. //
  132. // Initialise all "Show Movie" globals
  133. //----------------------------------------------
  134.  
  135. void InitGlobals(void)
  136. {
  137.     short windCount;
  138.     
  139. // init the windows list
  140.     for (windCount = 0; windCount < kMaxWindows; windCount++)
  141.         gTheWinds[windCount] = nil;
  142.  
  143.     gDone = false; // Set the done flag
  144.  
  145.     InitMovieGlobals();  // init the movie globals
  146.     InitMoviePrefs();    // init the movie prefs
  147.     MenuBarInit();       // init menubar
  148. }
  149.  
  150. //----------------------------------------------
  151. // KillGlobals
  152. //
  153. // Kills all "Show Window" Globals and closes the windows
  154. //----------------------------------------------
  155.  
  156. void KillGlobals(void)
  157. {
  158.     CloseAllWindows();
  159.     KillMovieGlobals();
  160.     KillMoviePrefs();
  161. }
  162.  
  163. //==============================================
  164. //  Event Loop and event handling 
  165. //==============================================
  166.  
  167. //----------------------------------------------
  168. //  HandleMouseDown
  169. //
  170. // This looks after a mouse down. It works out where
  171. // the mouse has clicked and then deals with it 
  172. // apropriately
  173. //----------------------------------------------
  174.  
  175. void HandleMouseDown(EventRecord *eventPtr);
  176. void HandleMouseDown(EventRecord *eventPtr)
  177. {
  178.     WindowPtr    window;
  179.     short        thePart;
  180.     long        menuChoice;
  181.     
  182. // Find out where the user has clicked
  183.     thePart=FindWindow (eventPtr->where,&window);
  184.     switch (thePart)
  185.     {
  186. // Handle a click in the menu bar
  187.         case inMenuBar:
  188.             menuChoice = MenuSelect (eventPtr->where); // Displays menus and returns a selection 
  189.             DoMenuCommand(menuChoice);                 // Respond to the menu choice
  190.             break;
  191.  
  192. // In a desk accesory window, so let the system deal with it
  193.         case inSysWindow:
  194.             SystemClick(eventPtr,window);
  195.             break;
  196.  
  197. // In the title bar (drag region)
  198. //    so drag the window around
  199.         case inDrag:
  200.             DragSelWind(window,eventPtr->where);
  201.  
  202. // In the go away button
  203. //    need to tracks the mouse and close the window if released in go away button
  204.         case inGoAway:
  205.             DoGoAwayWind(window,eventPtr->where);  
  206.             break;
  207.  
  208. // In the windows content area
  209.         case inContent:
  210.             {
  211.     // If this is not the front window, then bring the window to the front
  212.                 if (window!=FrontWindow())
  213.                 {
  214.                     SelectWindow(window);
  215.                     break;
  216.                 }
  217.             }
  218.         break;
  219.     }
  220. }
  221.  
  222.  
  223. //----------------------------------------------
  224. // DoDiskEvt
  225. //
  226. //   Handle disk inserted event
  227. //----------------------------------------------
  228.  
  229. void DoDiskEvt(EventRecord *eventPtr);
  230. void DoDiskEvt(EventRecord *eventPtr)
  231. {
  232.     short    errResult;
  233.     Point    errPoint;
  234.  
  235.     if((HiWord(eventPtr->message)!=noErr))
  236.     {
  237.         SetPt(&errPoint,100,100);
  238.         errResult=DIBadMount(errPoint,eventPtr->message);
  239.     }
  240. }
  241.  
  242. //----------------------------------------------
  243. // Event Loop
  244. //----------------------------------------------
  245.  
  246. void EventLoop (void)
  247. {
  248.     EventRecord    event;       // The event returned
  249.     char          theChar;     // what character the user pressed
  250.     long        menuChoice;  // Menu & item the user selected
  251.  
  252.     DoAdjustMenus();  // Adjust appropriately
  253.     
  254. // Keep going until the user decides to quit (i.e. gDone = true)
  255.     while (!gDone)
  256.     {
  257. // Change the cursor to an arrow
  258.         SetCursor( &(qd.arrow));
  259.  
  260. // Receive every event from the system
  261.         WaitNextEvent (everyEvent,&event,0,nil);
  262.  
  263. // Do anything that the movies needs to do, including clicking
  264. // in the movie control
  265.         if (!ServiceMovieTasks( FrontWindow(), &event ))
  266.         {
  267.  
  268. // If event still needs to handled then do switch
  269.             switch (event.what)
  270.             {
  271.     // null event, do nothing
  272.                 case nullEvent:
  273.                     break;
  274.  
  275.     // user has clicked so deal with it
  276.                 case mouseDown:
  277.                     HandleMouseDown(&event);
  278.                     break;
  279.  
  280.     // user has pressed a key
  281.                 case keyDown:
  282.                 case autoKey:
  283.  
  284.             // Extract the key pressed
  285.                     theChar=event.message & charCodeMask; 
  286.  
  287.             // If the Command key was down then compare it pass it
  288.                     if (event.modifiers & cmdKey)
  289.                     {
  290.                         menuChoice = MenuKey (theChar);   // Find out what menu was selected by the keypress
  291.                         DoMenuCommand( menuChoice  );     // Respond to the menu choice
  292.                     }
  293.                     break;
  294.  
  295.     // window needs to be updated, or redrawn
  296.                 case updateEvt:
  297.                     DoWindUpdate((WindowPtr)(event.message));
  298.                     break;
  299.  
  300.     // disk inserted event
  301.                 case diskEvt:
  302.                     DoDiskEvt(&event);
  303.                     break;
  304.  
  305.     // Operating system event
  306.                 case osEvt:
  307.                     break;
  308.             }
  309.         }
  310.         
  311.     }
  312.  
  313. }
  314.